package com.zqc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zqc.common.Const;
import com.zqc.common.R;
import com.zqc.entity.AddressBook;
import com.zqc.service.AddressBookService;
import com.zqc.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    public AddressBookService addressBookService;

    /**
     * 查询地址列表
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list() {
        Long userId = BaseContext.getCurrentId();
        List<AddressBook> bookList = addressBookService.list(new LambdaQueryWrapper<AddressBook>()
                .eq(AddressBook::getUserId, userId));
        return R.success(bookList);
    }

    /**
     * 新增地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook) {
        // 设置地址所属用户id
        addressBook.setUserId(BaseContext.getCurrentId());
        // 保存地址信息
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

    /**
     * 查询具体地址信息回显
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> getOne(@PathVariable(name = "id") Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    /**
     * 修改地址信息
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.updateById(addressBook);
        return R.success("修改地址成功");
    }

    /**
     * 删除地址
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long[] ids) {
        addressBookService.removeBatchByIds(Arrays.asList(ids));
        return R.success("删除地址成功");
    }

    @PutMapping("/default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
        // 将所有地址都设置成非默认地址
        addressBookService.update(new LambdaUpdateWrapper<AddressBook>()
                .eq(AddressBook::getUserId, BaseContext.getCurrentId())
                .set(AddressBook::getIsDefault, Const.NOT_DEFAULT));

        // 将该id地址设置为默认地址
        addressBook.setUserId(BaseContext.getCurrentId());
        // 设置默认地址
        addressBook.setIsDefault(Const.IS_DEFAULT);
        addressBookService.updateById(addressBook);

        return R.success(addressBook);
    }

    /**
     * 获取用户默认地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> defaultAddre(){
        AddressBook book = addressBookService.getOne(new LambdaQueryWrapper<AddressBook>()
                .eq(AddressBook::getUserId, BaseContext.getCurrentId())
                .eq(AddressBook::getIsDefault, Const.IS_DEFAULT));
        return R.success(book);
    }
}
